<?php
/**
 * alltosun.com 推送消息记录列表 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 雷健雄 (leijx@alltosun.com) $
 * $Date: 2015-2-26 上午11:05:21 $
 * $Id: index.php 211119 2015-02-28 07:12:27Z leijx $
 */


class Action
{
    private $per_page = 30;

    public function __call($action = '', $params = array())
    {
        $push_id = Request::getParam('push_id', 0);
        // 没有推送id跳转至推送列表
        if (!$push_id) {
            Response::redirect(AnUrl('push/admin', '&cache=0'));
            Response::flush();
        }

        $search_filter = array();
        $filter  = array();
        $order   = ' ORDER BY `id` DESC ';
        $page_no = Request::getParam('page_no', 1);
        $status  = Request::getParam('status', 1);

        $search_filter = Request::getParam('search_filter', array());
        $filter = array('1'=>1);
        $filter['status'] = $status;
        $filter['push_id'] = $push_id;

        /*
        $user_ids = _model('push_record')->getFields('user_id', array(1=>1));
        $user_ids = array_unique($user_ids);
        $user_list = _model('user')->getList($user_ids);

        // 得到得到昵称和系统消息id的一维数组
        $user_nick_names = array_to_option($user_list, 'nick_name');
        //系统消息昵称搜索
        if (isset($search_filter['nick_name']) && $search_filter['nick_name']) {
            foreach ($user_nick_names as $k => $nick_name) {
                if (strstr($nick_name, AnFilter::filter_string(trim($search_filter['nick_name'])))) {
                    $filter['user_id'][] = $k;
                }
            }
        }

        // 系统消息id搜索
        if (isset($search_filter['user_id']) && $search_filter['user_id']) {
            $filter_user_ids = explode(',', trim($search_filter['user_id'], ','));
            if ($filter['user_id']) {
                $filter['user_id'] = array_merge($filter_user_ids, $filter['user_id']);
            } else {
                $filter['user_id'] = $filter_user_ids;
            }
        }
        */

        if (isset($search_filter['start_add_time']) && $search_filter['start_add_time']) {
            $filter['add_time >= '] = $search_filter['start_add_time'].' 00:00:00';
        }
        if (isset($search_filter['end_add_time']) && $search_filter['end_add_time']) {
            $filter['add_time <= '] = $search_filter['end_add_time'].' 23:59:59';
        }

        $total = _model('push_record')->getTotal($filter);
        if ($total) {
            $list = get_data_list('push_record', $filter, $order, $page_no, $this->per_page);
            $push_record_list = array();
            $push_record_list = $list;
            Response::assign('push_record_list', $push_record_list);
        }

        Response::assign('filter', $filter);
        Response::assign('search_filter', $search_filter);
        Response::assign('status', $status);
        Response::display('admin/push_record_list.html');
    }

    /**
     * 推送广播消息
     */
    public function add_all()
    {
        Response::display('admin/push_add.html');
    }

    /**
     * 推送指定用户消息
     */
    public function add_reg()
    {
        $receiver_type = 1;

        Response::assign('receiver_type', $receiver_type);
        Response::display('admin/push_add.html');
    }

    /**
     * 保存推送记录，并执行推送
     * @return unknown|multitype:string
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        // 标识再次发送
        // $id = Request::getParam('id', '');

        foreach ($info as $k => $v) {
            if (!is_numeric($v)) {
                $info[$k] = AnFilter::filter_string(trim($v));
            }
        }

        $msg = array();
        if (!$info['content'] || str_cn_len($info['content']) > 25) {
            return '请正确填写消息内容';
        }
        $msg['content'] = $info['content'].date('H:i:s');

        if ($info['res_name']) {
            // @todo 具体的res_name选项
            $push_res_name = array();
            if (!array_key_exists($info['res_name'], $push_res_name)) {
                // return '请传递合法的分类';
            }
            $msg['res_name'] = $info['res_name'];
        }

        if ($info['res_id']) {
            $msg['res_id'] = $info['res_id'];
        }
        if ($info['link']) {
            $msg['link'] = $info['link'];
        }

        $user_ids = array();
        if ($info['receiver_type']) {
            $info['receiver_type'] = 5;
            $user_ids = explode(',', trim($info['user_id']));
        } else {
            $info['receiver_type'] = 4;
        }

        // 拼装数据
        $data = array();
        $data['receiver_type'] = $info['receiver_type'];
        $data['user_id']  = $info['user_id'];
        $data['content']  = $info['content'];
        $data['res_name'] = $info['res_name'];
        $data['res_id']   = $info['res_id'];
        $data['status']   = 1;
        $data['add_time'] = date('Y-m-d H:i:s');

        // 推送
        $result = _widget('push')->send($msg, $user_ids);
        if (is_string($result)) {
            $data['status'] = 2;
            _model('push_record')->create($data);
            return $result;
        }
        _model('push_record')->create($data);
        return array('操作成功', 'success', AnUrl('push/admin'));
    }

    /**
     * 删除操作
     * @return string
     */
    public function delete()
    {
        $id = Request::getParam('id', '');
        $ids = explode(',', trim($id, ','));

        $count = count($ids);
        if ($count < 1) {
            return '操作id不准确';
        }

        $push_list = _model('push_record')->getList($ids);
        foreach ($push_list as $push_info) {
            if (!$push_info || !in_array($push_info['id'], $ids)) {
                continue;
            }
            if (1 != $push_info['status']) {
                return '该系统消息已经删除了';
            }
            $control = _model('push_record')->update($push_info['id'], array('status'=>0));
            if ($control) {
                // _widget('log')->record('push_record', $id, '修改');
            }
        }
        return 'ok';
    }

    public function recover()
    {
        $id = Request::getParam('id', '');
        $push_info = _uri('push_record', $id);

        if (!$push_info || !$id) {
            return '操作id不准确';
        }

        if (0 != $push_info['status']) {
            return '该推送消息已经还原';
        }
        $control = _model('push_record')->update($push_info['id'], array('status'=>1));
        if ($control) {
            // _widget('log')->record('push_record', $push_info['id'], '修改');
        }
        return 'ok';
    }

    public function thorough_delete()
    {
        $id = Request::getParam('id', 0);
        $push_info = _uri('push_record', $id);

        if (!$id) {
            return '请传递要操作的id';
        }
        if (!$push_info) {
            return '要操作的信息不存在';
        }

        $control = _model('push_record')->delete($id);
        if ($control) {
            // _widget('log')->record('push_record', $id, '删除');
            return 'ok';
        }
        return '操作失败';
    }
}
?>